package com.dengyuanke.admin.controller;


import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.dengyuanke.common.result.JsonResult;
import com.dengyuanke.entity.Doctor;
import com.dengyuanke.service.IDoctorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2017-11-13
 */
@Api(tags = "医生相关API")
@RestController
public class DoctorController {

    @Autowired
    private IDoctorService doctorService;

    /**
     * 添加医生
     * @param doctor
     * @return
     */
    @ApiOperation(value = "添加医生",notes = "传入医生实体")
    @PostMapping("/adddoctor")
    public JsonResult addDoctor(Doctor doctor){
        doctor.setId(IdWorker.get32UUID());
        doctor.setCreateTime(new Date());
        boolean insert = doctorService.insert(doctor);
        if(insert){
            return JsonResult.renderSuccess("添加成功");
        }else {
            return JsonResult.renderFail("添加失败");
        }
    }

    /**
     * 更新医生
     * @param doctor
     * @return
     */
    @ApiOperation(value = "更新医生",notes = "传入带id的医生实体")
    @PostMapping("/updatedoctor")
    public JsonResult updateDoctor(Doctor doctor){
        if(StringUtils.isEmpty(doctor.getId())){
            return JsonResult.renderFail("未传入id,更新失败");
        }
        doctor.setUpdateTime(new Date());
        boolean insert = doctorService.updateById(doctor);
        if(insert){
            return JsonResult.renderSuccess("更新成功");
        }else {
            return JsonResult.renderFail("更新失败");
        }
    }

    @ApiOperation(value = "获取所有医生",notes = "不需要传入参数")
    @PostMapping("/getdoctors")
    public JsonResult getDoctors(){
        return JsonResult.renderSuccess(doctorService.selectList(null));
    }
	
}
